package com.hmall.item.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hmall.common.dto.PageDTO;
import com.hmall.item.constants.MqConstants;
import com.hmall.item.mapper.ItemMapper;
import com.hmall.item.pojo.Item;
import com.hmall.item.pojo.ItemDoc;
import com.hmall.item.service.IItemService;
import org.apache.ibatis.annotations.Param;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.security.InvalidParameterException;
import java.util.List;

@Service
public class ItemService extends ServiceImpl<ItemMapper, Item> implements IItemService {
    @Autowired
    private ItemMapper itemMapper;
    @Autowired
    private RabbitTemplate rabbitTemplate;
    /**
     * 分页查询商品
     *
     * @param page
     * @param pageSize
     * @return
     */
    @Override
    public PageDTO<Item> pageIofo(Integer page, Integer pageSize) {
        //分页构造器
        Page<Item> pageInfo = new Page<>(page, pageSize);
        //查询构造器
        LambdaQueryWrapper lqw = new LambdaQueryWrapper<>();
        itemMapper.selectPage(pageInfo, lqw);
        long total = pageInfo.getTotal();
        List<Item> records = pageInfo.getRecords();
        PageDTO<Item> pageDTO = new PageDTO<>();
        pageDTO.setList(records);
        pageDTO.setTotal(total);
        return pageDTO;
    }

    /**
     * 商品上架、下架
     *
     * @param id
     * @param status
     */
    @Override
    public void sxj(Long id, Integer status) {
        Item item = new Item();
        item.setId(id);
        item.setStatus(status);
        itemMapper.updateById(item);
        if (status.equals(1)){
            rabbitTemplate.convertAndSend(MqConstants.HMALL_EXCHANGE,MqConstants.HMALL_INSERT_KEY,id);
        }else if(status.equals(2)){
            rabbitTemplate.convertAndSend(MqConstants.HMALL_EXCHANGE,MqConstants.HMALL_DELETE_KEY,id);
        }

    }
/*
    @Override
    public void save(ItemDoc itemDoc) {
        rabbitTemplate.convertAndSend(MqConstants.HMALL_EXCHANGE,MqConstants.HMALL_INSERT_KEY,itemDoc.getId());
    }*/

   /* @Override
    public void updateById(ItemDoc itemDoc) {
        if (itemDoc.getId() == null) {
            throw new InvalidParameterException("id不能为空");
        }
        rabbitTemplate.convertAndSend(MqConstants.HMALL_EXCHANGE,MqConstants.HMALL_INSERT_KEY,itemDoc.getId());
    }
*/
    @Override
    public void removeById(Long id) {
        rabbitTemplate.convertAndSend(MqConstants.HMALL_EXCHANGE,MqConstants.HMALL_DELETE_KEY,id);
    }

    /**
     * 扣减库存
     * @param itemId
     * @param num
     */
    @Transactional
    @Override
    public void reduceStock(Long itemId, Integer num) {
        Item item = itemMapper.selectById(itemId);
        item.setStock(item.getStock()-num);
        itemMapper.insert(item);
    }


}
